<template>
	<view>
		<image class="bg-set" :src="pagebgImage"></image>
		<!-- 顶部导航 -->
		<fa-navbar :isBackIndex="isBackIndex" :title="vuex_table_title || '在线调研'"></fa-navbar>
		<view class="form-mart " v-if="showInfo">
			<view class="banner" v-if="showBaner">
				<view class="title" v-if="title" >
					{{title}}
				</view>
				<view class="investigateInfo" v-if="investigateInfo" v-html="investigateInfo">
				</view>
				<view class="form-item" id="form">
					<u-form-item labelWidth="auto" :label-position='item.type == "select"?"left":"top"' v-for="(item,index) in form" :key='index' @click="onclick">
						<view class="" style="font-size: 36rpx;">
							{{item.title}}
						</view>
						<u-radio-group v-model="item.answer" v-if="item.type == 'radio'" @change="groupChange(index)" size="40" >
						 	<u-radio iconColor="" v-for="(jtem, i) in form[index].value" :key="i" :name="jtem" @change="radioChange" :disabled="disabled">
						 		{{ jtem }}
						 	</u-radio>
						 </u-radio-group>
						 <view class="" v-if="item.type == 'input' ">
						 	<u-input v-model=" item.answer"  border="true" @input="descInput(index)" :disabled="disabled"/>
						 </view>
						 <view class="select" v-if="item.type == 'select' " @click="form[index].show = is_answer?false:true" >
							{{ item.answer}}
							<u-icon name="arrow-down"></u-icon>
							<u-select v-model="item.show" :list="form[index].value" @confirm="confirm(index,$event)" size="40"></u-select>
						 </view>
						<u-checkbox-group v-if="item.type == 'checkbox'" :disabled="disabled" size="40">
						 	<u-checkbox v-model="jtem.checked" v-for="(jtem, i) in form[index].value" :key="i" :name="jtem.name" @change="checkboxChange" >
						 		{{ jtem.name }} 
						 	</u-checkbox>
						 </u-checkbox-group>
					 </u-form-item>
				</view>
				<view class="">
				</view>
				<view class="btn">
					<button type="default" :class="disabled?'isSubmit':'notSubmit'" v-if="!disabled" @click="submitStr">提交</button>
				</view>
			</view>
		</view>
		<view class="content" v-else>
			<u-image width="360rpx" border-radius="6" height="300rpx" :src="bgImage" ></u-image>
			<view class="content_txt">
				暂无在线调研信息
			</view>
		</view>
		<view class="goIndex" @click="goIndex">
			<u-icon name="home" size="40"></u-icon>
		</view>
	</view>
</template>

<script>
import formMart from '@/pages/activity/form.vue'
export default {
	components:{
		formMart
	},
	computed: {
	},
	data() {
		return {
			form:null,
			id:151,
			showInfo:false,
			bgImage:this.$image_url+'/uploads/uniapp/image/notNews.png',
			pagebgImage:this.$image_url+'/uploads/uniapp/image/Investigate.png',
			request_item: [],
			info:{},
			investigateInfo:'',
			desc:'',
			conSelect:false,
			selectTxt:'',
			radioTxt:'',
			checkedTxt:'',
			inputTxt:'',
			showBaner:true,
			isSubmit:false,
			ids:0,
			radio: '',
			title:'',
			question_id:'',
			answerArr:[],
			is_answer:'',
			asdf:[],
			checkboxArr:[],
			selectArr:[],
			inputArr:[],
			radioIndex:null,
			show:false,
			disabled:false,
			isBackIndex:2
		}
	},
	onLoad(e) {
		// this.getQuestionList()
		console.log(e,'获取的id')
		this.id = e.id
		this.getInfo(this.id)
	},

	onShow() {
		// if(this.form.length){
		// 	this.formArr = this.form
		// 	console.log(this.form,'formformformformformformformform')
		// }
	},
	methods: {
		confirm(index,e){
			this.conSelect = true;
			this.form.forEach((item,j)=>{
				if(item.type == "select"){
					this.form[index].answer = e[0].label
				}
			})
		},
		checkboxChange(e){
			console.log(e,'checkboxcheckbox');
		},
		onShowSel(indexs){
		},
		radioChange(e){
			console.log(this.form,'radioChange');
		},
		groupChange(index){
			this.radioIndex = index
		},
		descInput(index){
			this.form[index].answer = this.form[index].answer
			console.log(this.form[index].answer,'descInput');
			
		},
		goIndex(){
			this.$u.route('/pages/index/index');
		},
		onClick(index){
			console.log(index,'index')
		},
		async submitStr(){
			if(this.disabled){
				uni.showToast({
				title: '已提交过一次，无法再次提交',
				icon:"none"
				});
				return
			}else{
				let that = this
				let checkedTxt = ''
				let checkedArr = []
				that.form.forEach((item,index)=>{
						if(item.type == "checkbox"){
							let checkBox = []
							item.value.forEach((jtem,jndex)=>{
								let check = []
								if(jtem.checked){
									check.push(jtem.name)
									checkBox.push(...check).toString()
								}
							})
							item.answer = checkBox.toString()
						}else if(item.type == "input"){
							item.answer = item.answer.trim()
						}else if(item.type == "select"){
						}
				})
				that.isSubmit = that.form.every((item,index)=>{
					 return item.answer !== ''
				})
				console.log(that.isSubmit,'that.isSubmit')
				if(that.isSubmit){
						let data = {
							question_id: this.question_id,
							form_json: this.form
						}
					let res = await this.$api.questAnswer(data);
						if(res.code){
							uni.showToast({
							title: res.msg,
							icon:"none"
							});
							setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/activity/investigate'
							})
							},2000)
						}else {
							uni.showToast({
							title: res.msg,
							icon:"none"
							});
							this.showBaner = false
							setTimeout(()=>{
								uni.redirectTo()({
									url:'/pages/activity/investigate'
								})
							},2000)
						}
				}else{
					uni.showToast({
					title: '请完成全部调研内容后进行提交',
					icon: 'none'
					});
					that.isSubmit = false				
				}
			}
		
		},
     async getInfo(id) {
		let that = this
        let data = {
          question_id: id
        };
     
        let res = await this.$api.questInfo(data);
		// let res = await this.$api.getActivity(data);
     
        if (res.code && res.data) {
			if(!res.data.is_answer){
				console.log(!res.data.is_answer,'res.data.is_answer')
				this.showInfo = true
				let form = res.data.form_json
				let ids = this.id
				let valueArr = []
				let answerArr = []
				this.investigateInfo = res.data.intro.replace("&nbsp;","\u3000")
				this.title = res.data.title
				this.question_id = res.data.id
				this.is_answer = res.data.is_answer
				if(form.length != 0){
					form.forEach((item,j)=>{
						let answer = {}
						answer.type = item.type
						answer.answer = ''
						if(item.answer){
							return
						}else {
							if(item.type == 'select'){
								let value = []
								let selectArr = []
								// let show = {show:false}
								Object.assign(item,{show:false})
								console.log(item,'itemitemitemitemitemitem1111')
							  item.value.forEach((jtem,index)=>{
								  let valueObj1 = {}
								  valueObj1.value = index
								  valueObj1.label = jtem
								  value.push(valueObj1)
								  item.value = []
								  item.value.push(...value)
							  })
							  item.answer = item.value[0].label  
							  console.log(item,'itemitemitemitemitemitem')
							}else if(item.type == 'checkbox') {
								let value = []
								let checkboxArr = []
							  item.value.forEach((jtem,index)=>{
							  		 let valueObj2 = {}
							  		 valueObj2.name = jtem
							  		 valueObj2.checked = false
									 valueObj2.disabled = false
							  		 value.push(valueObj2)
									 item.value = []
									 item.value.push(...value)
							  })
							}else if(item.type == 'input'){
								let value = []
								let inputArr = []
								// that.inputArr.push(value)
								// item.inputArr.push(value)
								// console.log(form,'checkboxValue3333333')
							}else if(item.type == 'radio'){
									item.radioTxt = ''
							}
													
						}
						answerArr.push(answer)
					})
					this.form = form
					that.answerArr = answerArr
				}
				this.info = res.data
			}else{
				let that = this
				this.showInfo = true
				this.disabled = true
				this.is_answer = res.data.is_answer
				console.log(!res.data.is_answer,res.data.is_answer,'res.data.is_answer')
				that.form = res.data.form_json
				that.investigateInfo = res.data.intro.replace("&nbsp;","\u3000")
				console.log(that.form,'formformformformformform')
				// that.form.forEach((item,index)=>{
				// 	if(item.type == "checkbox"){
				// 		item.forEach((jtem,jndex)=>{
				// 			that.checkboxArr.push(jtem.value)
				// 		})
				// 	}else if(item.type == "radio"){
				// 		that.radioTxt = item.answer
				// 	}else if(item.type == "input"){
				// 		item.answer = that.inputTxt.trim()
				// 	}else if(item.type == "select"){
				// 		// item.answer = that.conSelect?that.selectTxt:item.value[0].label 
				// 	}
				// })
				that.title = res.data.title
				that.showInfo = true
			}

        } else {
		  this.showInfo = false
          uni.showToast({
            title: res.msg ? res.msg : '数据有误',
			icon:"none"
          });
     
        }
     },
	
	},
	onPageScroll(e) {
	},
	//下拉刷新
	onPullDownRefresh() {
	},
	onReachBottom() {
	}
};
</script>

<style lang="scss">
	page{
		// background-color: #ed1e28;
		.content {
			width: 648rpx;
			height: 800rpx;
			margin: 0 auto;
			border-radius: 20rpx;
			margin-top: 200rpx;
			margin-bottom: 90rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			flex-direction: column;
			padding-top: 140rpx;
			.content_txt{
				margin-top: 70rpx;
				font-size: 28rpx;
				color: #999999;
				
			}
		}
		#form .u-radio__icon-wrap--disabled--checked{
			background-color: #ccc
		}
		#form .u-checkbox__icon-wrap--disabled--checked{
			background-color: #ccc
		}
		.isSubmit{
			color: #de0c1f;
			background-color: #fff;
		}
		.notSubmit{
			color: #fff;
			background-color: #de0c1f;
		}
		.u-radio{
			margin-bottom: 10rpx;
		}
		.form-mart{
			position: relative;
			width: 85%;
			height: 800rpx;
			margin: 0 auto;
			padding-top: 140rpx;
		}
		.bg-set {
			position: fixed;
			width: 102%;
			height: 100%;
			top: 130rpx;
			margin-left:-4rpx;
			left: 0;
			z-index: -1;
		}
		.banner {
			position: absolute;
			left: 0;
			top: 0;
			background-color: #FFFFFF;
			width: 100%;
			border-radius: 20rpx;
			padding: 30rpx;
			height: 1100rpx;
			margin-top: 150rpx;
			// overflow: auto;
			.form-item{
				height: 800rpx;
				overflow: auto;
			}
			.btn{
				position: absolute;
				width: 100%;
				bottom: 30rpx;
				left: 0;
				padding: 0 30rpx;
				color:#fff;
			}
			.title{
				font-size: 40rpx;
				color: #323232;
				font-weight: bold;
				text-align: center;
				margin-bottom: 20rpx;
			}
			.investigateInfo{
				white-space: pre-wrap;
			}
			.u-input{
				// width: 100rpx !important;
			}
			.textarea_box{
				position: relative;
				.textarea{
					height: 100rpx;
					width: 100%;
					background-color: #999;
					padding: 20rpx 0;
				}
				.num{
					position: absolute;
					display: inline-block;
					bottom: 0;
					right: 0;
				}
			}
			.select{
				width: 150rpx;
				display: flex;
				justify-content: space-around;
				border: 2rpx solid #dcdfe6;
			}
			
		}
		.goIndex{
			position: fixed;
			right: 30rpx;
			top: 180rpx;
			width: 80rpx;
			height: 80rpx;
			border-radius: 60rpx;
			font-size: 26rpx;
			text-align: center;
			background-color: #d9001b;
			color: white;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			align-items: center;
		}
	}
</style>
